package edu.ustb.eureka.contract;

import edu.ustb.sc.definitions.ContractActionInfo;
import edu.ustb.sc.definitions.ContractPartyInfo;
import edu.ustb.sc.definitions.SpescConditionCheckStatus;
import edu.ustb.sc.definitions.SpescContractTerm;
import edu.ustb.sc.definitions.SpescTermActionQualifierEnum;
import edu.ustb.sc.definitions.TermLogicEnum;

/**
 * term_2: EUREKA must Discover()
 * 	when Customer did request(Discover)
 * 	while ForecastWeather.rights.useRight.add(Customer)
 * 	where response.code = 204
 */
public class DiscoveryContractTerm2 extends SpescContractTerm<DiscoveryContract> {

	public DiscoveryContractParty partyInfo = new DiscoveryContractParty("EUREKA");

	public static SpescTermActionQualifierEnum qualifier = SpescTermActionQualifierEnum.Must;

	public static String actionName = "register";

	public DiscoveryContractTerm2() {
	}

	public DiscoveryContractTerm2(ContractActionInfo actionInfo) {
		this.actionInfo = actionInfo;
	}

	@Override
	public TermLogicEnum getLogic() {
		return TermLogicEnum.Conditional;
	}

	@Override
	public String getTermName() {
		return "term_2";
	}

	@Override
	public SpescTermActionQualifierEnum getQualifier() {
		return qualifier;
	}

	@Override
	public ContractPartyInfo getPartyInfo() {
		return partyInfo;
	}

	/**
	 *
	 * @param contract
	 * @param param
	 * @return
	 */
	@Override
	public SpescConditionCheckStatus whenPreCondition(DiscoveryContract contract, Object... param) {
		try {
			ContractActionInfo requestAction = new ContractActionInfo(DiscoveryContract.class
					.getDeclaredMethod("request"),
					new Object[0]);
			if (contract.isActionDid(requestAction)) {
				return SpescConditionCheckStatus.Yes;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SpescConditionCheckStatus.No;
	}

	/**
	 *
	 * @param contract
	 * @param param code
	 * @return
	 */
	@Override
	public SpescConditionCheckStatus wherePostCondition(DiscoveryContract contract, Object... param) {
		if (param[0] != null) {
			int code = Integer.parseInt(param[0].toString());
			if (code >= 200 && code < 300) {
				return SpescConditionCheckStatus.Yes;
			}
			return SpescConditionCheckStatus.No;
		}
		return SpescConditionCheckStatus.Unknown;
	}

	public String getActionName() {
		return actionName;
	}
}
